package com.neusoft.neu24.nep.role.service.impl;

import com.neusoft.neu24.nep.entity.ComonResult;
import com.neusoft.neu24.nep.role.entity.Roles;
import com.neusoft.neu24.nep.role.entity.Sysnode;
import com.neusoft.neu24.nep.role.mapper.RolesMapper;
import com.neusoft.neu24.nep.role.service.IRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 段静迪，duanjingdi@163.com
 * @since 2024-06-05
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {

    @Override
    public ComonResult< List<Roles>> listAll() {
        List<Roles> lrs =  getBaseMapper().selectList(null);
        return ComonResult.success(lrs);
    }

    @Override
    public ComonResult modRoleByid(Roles roles) {
        int i =  getBaseMapper().updateById(roles);
        return ComonResult.success(i);
    }

    @Override
    public ComonResult addRole(Roles roles) {
//        如果使用数据库表的ID自增，那么ID列必须为null
        roles.setId(null);
        int i =  getBaseMapper().insert(roles);
//        此处省略500行。。。。。。。。
        return ComonResult.success(roles.getId());
    }

    @Override
    public ComonResult modRole(Roles roles) {
        int i = getBaseMapper().updateById(roles);
        return ComonResult.success(i);
    }

    @Override
    public ComonResult selectSysnodesByRid(Integer rid) {
        List<Integer> list = getBaseMapper().selectSysnodesByRid(rid);
        return ComonResult.success(list);
    }

    @Override
    public ComonResult modRoleAuth(Integer rid, List<Integer> sysnodeids) {



        if(sysnodeids != null && sysnodeids.size() > 0){
//            删除角色和系统功能的关系表中的旧数据
            int i =   getBaseMapper().deleteRoleAuth(rid);
            i =  getBaseMapper().insertRoleAuth(rid,   sysnodeids);
            return  ComonResult.success(i);
        }
        return ComonResult.failed(555,"授权失败");
    }

    @Override
    public ComonResult selectMenuByRoleid(Integer roleid) {
        List<Sysnode> lss =  getBaseMapper().selectMenuByRoleid(roleid);
        return ComonResult.success(lss);
    }
}
